unit SmallStruct.BusinessObject.TSSScriptBusinessObject;

interface

uses
  Classes,
  SmallStruct.Script,
  SmallStruct.BusinessObject,
  SmallStruct.BusinessObject.TSSAdvancedBusinessObject,
  SmallStruct.Script.TSSScript,
  atPascal,
  SmallStruct.Tools,
  SmallStruct.Core,
  SmallStruct.Database,
  SmallStruct.Database.TSSADODatabaseDriver;

type
  TSSScriptBusinessObjectContext = class(TSSAdvancedBusinessObjectContext, ISSScriptBusinessObjectContext)
  private
    _Script: ISSScript;
    _Parameters: ISSRecord;
  public
    function GetScript: ISSScript;
    function GetParameters: ISSRecord;
    procedure SetScript(AScript: ISSScript);
    procedure SetPrameters(Parameters: ISSRecord);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TSSScriptBusinessObject = class(TSSAdvancedBusinessObject, ISSScriptBusinessObject)
  private
    _ScriptEngine: TatPascalScripter;
  public
    function Execute(Parameters: ISSRecord): Variant;
  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

uses
  Dialogs,
  ap_Classes,
  SmallStruct.Script.ScriptObject,
  SmallStruct.Configuration,
  SmallStruct.Core.TSSObjectContainer;

{ TSSScriptBusinessObjectContext }

constructor TSSScriptBusinessObjectContext.Create(AOwner: TComponent);
begin
  inherited;
  _Parameters := TSSRecord.Create(Self);
end;

destructor TSSScriptBusinessObjectContext.Destroy;
begin
  _Parameters.ReleaseObjectInstance;
  inherited;
end;

function TSSScriptBusinessObjectContext.GetParameters: ISSRecord;
begin
  Result := _Parameters;
end;

function TSSScriptBusinessObjectContext.GetScript: ISSScript;
begin
  Result := _Script;
end;

procedure TSSScriptBusinessObjectContext.SetPrameters(Parameters: ISSRecord);
begin
  _Parameters := Parameters;
end;

procedure TSSScriptBusinessObjectContext.SetScript(AScript: ISSScript);
begin
  _Script := AScript;
end;

{ TSSScriptBusinessObject }

constructor TSSScriptBusinessObject.Create(AOwner: TComponent);
begin
  inherited;
  _ScriptEngine := TatPascalScripter.Create(Self);
end;

function TSSScriptBusinessObject.Execute(Parameters: ISSRecord): Variant;
begin
  (GetContext.GetObjectInstance as TSSScriptBusinessObjectContext)._Parameters := Parameters;

  _ScriptEngine.AddLibrary(TatClassesLibrary);
  _ScriptEngine.AddLibrary(TSSObjectContainerSO);
  _ScriptEngine.AddLibrary(TSSRecordSO);
  _ScriptEngine.AddLibrary(TSSDataSetSO);
  _ScriptEngine.AddLibrary(TSSDataModelSO);
  _ScriptEngine.AddLibrary(TSSDatabaseDriverSO);
  _ScriptEngine.AddLibrary(TSSScriptBusinessObjectContextSO);

  _ScriptEngine.SourceCode.Text := (GetContext as ISSScriptBusinessObjectContext).GetScript.GetScript.Text;

  //  _ScriptEngine.Compiled;

  Result := _ScriptEngine.ExecuteSubroutine('_MAIN', [GetContext.GetObjectInstance]);
end;

end.
